﻿using AutoMapper;
using MediatR;
using SmartCity.ProcessEngine.API.Read.Application.Commands.FormDetail;
using SmartCity.ProcessEngine.API.Read.DTO;
using SmartCity.ProcessEngine.Domain;
using SmartCity.ProcessEngine.Errorcode;
using SmartCity.ProcessEngine.Infrastructure.Interfaces;

namespace SmartCity.ProcessEngine.API.Read.Application.CommandHandlers.FormDetails
{
    public class FormDetailQueryCommandHandler:IRequestHandler<FormDetailQueryCommand, ApiResult<List<FormDetailDto>>>
    {
        private readonly IBaseRepository<WF_FormDetail> detailRep;
        private readonly IMapper mapper;

        public FormDetailQueryCommandHandler(IBaseRepository<WF_FormDetail> detailRep,IMapper mapper)
        {
            this.detailRep = detailRep;
            this.mapper = mapper;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回到明细列表任务</returns>
        public Task<ApiResult<List<FormDetailDto>>> Handle(FormDetailQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<FormDetailDto>> result = new ApiResult<List<FormDetailDto>>();
            result.code = ApiEnum.SearchSuccess;
            result.message = "查询成功";

            var list = detailRep.GetAll().Where(x=>x.FormNo == request.FormNo && !x.IsDeleted).ToList();
            result.data = mapper.Map<List<FormDetailDto>>(list);

            return Task.FromResult(result);
        }
    }
}
